//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//

// Copyright (c) 2002 BSQUARE Corporation.  All rights reserved.
// DO NOT REMOVE --- BEGIN EXTERNALLY DEVELOPED SOURCE CODE ID 40973--- DO NOT REMOVE

// Core header file for SD Card DDK

#ifndef _SDCARDDDK_H_
#define _SDCARDDDK_H_

#include "SDCard.h"

#define SD_CSD_VERSION_CODE_SUPPORTED   0 // version 1.0
#define MMC_CSD_VERSION_CODE_SUPPORTED  2 // version 3.1
#define DEFAULT_BLK_LENGTH              0x200

typedef PVoid SD_DEVICE_HANDLE;


// transfer class enum, indicates whether a bus request is read,write or a command
typedef enum {
    SD_READ = 0,         // block read
    SD_WRITE = 1,        // block write
    SD_COMMAND = 2,      // command only
} SD_TRANSFER_CLASS;

// SDIO Read/Write Enum
typedef enum {
    SD_IO_READ = 0,
    SD_IO_WRITE = 1,
} SD_IO_TRANSFER_TYPE;

// indicates Interface being used for a card
typedef enum {
    SD_INTERFACE_SD_MMC_1BIT = 0,
    SD_INTERFACE_SD_4BIT = 1,
} SD_INTERFACE_MODE;

// enum for querying card information, some of this information is cached
typedef enum {
    SD_INFO_REGISTER_OCR = 0,       // 32-bit OCR cached value for MMC/SDMemory Cards
    SD_INFO_REGISTER_CID,           // CID for MMC/SDMemory Cards
    SD_INFO_REGISTER_CSD,           // CSD for MMC/SDMemory Cards
    SD_INFO_REGISTER_RCA,           // RCA data type for MMC/SDMemory/SD IO Cards
    SD_INFO_REGISTER_IO_OCR,        // 24 bit IO-OCR register for SDIO Cards
    SD_INFO_REGISTER_SCR,         // card interface for all card types
    SD_INFO_CARD_STATUS,            // card status for MMC/SD Memory cards
    SD_INFO_SELECT_BUSWIDTH,
    SD_INFO_SELECT_BLKLENGTH,
    SD_INFO_HOST_IF_CAPABILITIES,   // SD Host Interface Capabilities
    SD_INFO_HOST_BLOCK_CAPABILITY,  // SD Host Block Length Capabilities
    SD_INFO_TYPE_COUNT              // number of SD_INFO_TYPEs
} SD_INFO_TYPE;

// enum for device type
typedef enum _SDCARD_DEVICE_TYPE {
    Device_Unknown = 0,
    Device_MMC = 1,
    Device_SD_Memory = 2,
    Device_SD_IO = 3,
    Device_SD_Combo = 4  // combo is only an intermediate device type reserved only for the bus driver
}SDCARD_DEVICE_TYPE, *PSDCARD_DEVICE_TYPE;

// enum for setting SD Card Features
typedef enum {
    SD_IO_FUNCTION_ENABLE = 0,          // enable I/O Function, uses SD_IO_FUNCTION_ENABLE_INFO
    SD_IO_FUNCTION_DISABLE,             // disable I/O Function - no structure required
    SD_IO_FUNCTION_SET_BLOCK_SIZE,      // set the SDIO Function block size,takes a UInt32 parameter
    SD_SET_DATA_TRANSFER_CLOCKS,        // set read and write data transfer clocks
    SD_SET_CARD_INTERFACE,              // set the physical card interface
    SD_SET_CLOCK_STATE_DURING_IDLE,     // set the clock state during the idle state
} SD_SET_FEATURE_TYPE, *PSD_SET_FEATURE_TYPE;

// structure for SD_IO_FUNCTION_ENABLE card feature
typedef struct _SD_IO_FUNCTION_ENABLE_INFO{
    UInt32   ReadyRetryCount;    // number of retries to check the ready bit in the function
    UInt32   Interval;           // interval in milliseconds to check
}SD_IO_FUNCTION_ENABLE_INFO, *PSD_IO_FUNCTION_ENABLE_INFO;

// info for SD_SET_DATA_TRANSFER_CLOCKS card feature
typedef struct _SD_DATA_TRANSFER_CLOCKS {
    UInt32   ReadClocks;         // the worst case number of read clocks before read data is sent
    UInt32   WriteClocks;        // number of clocks that busy signal can remain assrted
} SD_DATA_TRANSFER_CLOCKS, *PSD_DATA_TRANSFER_CLOCKS;

// structure for information about a card's interface
typedef struct _SD_CARD_INTERFACE {
    SD_INTERFACE_MODE   InterfaceMode;  // interface mode
    UInt32               ClockRate;      // clock rate
    Boolean                WriteProtected; // write protect flag (SD Memory cards)
} SD_CARD_INTERFACE, *PSD_CARD_INTERFACE;


// structure for SDIO device information
typedef struct _SD_CARD_INFO{
    UInt32        CardCapability;   //card capabilities
    UInt32        MaxBlockLength;
    UInt32        EraseSectorSize;
} SD_CARD_INFO, *PSD_CARD_INFO;

// structure for SD_INFO_HOST_BLOCK_CAPABILITY card information
typedef struct _SD_HOST_BLOCK_CAPABILITY{
    UInt16  ReadBlockSize;   // block size for read requests
    UInt16  WriteBlockSize;  // block size for write requests
    UInt16  ReadBlocks;      // number of blocks for each read request
    UInt16  WriteBlocks;     // number of blocks for each write request
} SD_HOST_BLOCK_CAPABILITY, *PSD_HOST_BLOCK_CAPABILITY;

// enum for response types
typedef enum  _SD_RESPONSE_TYPE {
    NoResponse  = 0,
    ResponseR1  = 1,
    ResponseR1b = 2,
    ResponseR2  = 3,
    ResponseR3  = 4,
    ResponseR4  = 5,
    ResponseR5  = 6,
    ResponseR6  = 7
} SD_RESPONSE_TYPE, *PSD_RESPONSE_TYPE;

// fast path event callback event types
typedef enum _SD_SLOT_EVENT_TYPE {
    SDCardEjected = 1,         // card ejected
}SD_SLOT_EVENT_TYPE, *PSD_SLOT_EVENT_TYPE;


typedef struct _SD_COMMAND_RESPONSE {
    SD_RESPONSE_TYPE ResponseType;                                  // response type
    UInt8            ResponseBuffer[SDCARD_RESPONSE_BUFFER_BYTES];  // largest possible reponse buffer
}SD_COMMAND_RESPONSE, *PSD_COMMAND_RESPONSE;


// request flags
#define SD_AUTO_ISSUE_CMD12     0x00000001  // automatically issue CMD12 after the command
#define SD_SDIO_AUTO_IO_ABORT   0x00000002  // (SDIO Only) automatically abort I/O using CMD52
// unspecified access clocks
#define SD_UNSPECIFIED_ACCESS_CLOCKS 0xFFFFFFFF
// bit fields for client registration flags
#define SD_CLIENT_HANDLES_RETRY 0x00000001         // client driver handles retries

// parsed CID structure
typedef struct _SD_PARSED_REGISTER_CID{
    UInt8  ManufacturerID;              // 8 bit manufacturer ID
    AChar   OEMApplicationID[2];         // 2 character ASCII
    AChar   ProductName[6];              // product name string 6 (to accomodate MMC cards) ASCII chars + 1 nul char
    UInt8  ProductRevision;             // 8 bit
    UInt32  ProductSerialNumber;         // 32 bit product serial number
    UInt16 ManufactureDate;             // Decoded year, range 2000 upwards
} SD_PARSED_REGISTER_CID, *PSD_PARSED_REGISTER_CID;

// file System type present on card
typedef enum {
    SD_FS_FAT_PARTITION_TABLE,
    SD_FS_FAT_NO_PARTITION_TABLE,
    SD_FS_UNIVERSAL,
    SD_FS_OTHER
} SD_FS_TYPE, *PSD_FS_TYPE;

// parsed CSD structure
typedef struct _SD_PARSED_REGISTER_CSD {
    UInt8 CSDVersion;             // 2 bit CSD version
    UInt8 TAAC;                   // The TAAC field in units of nano-seconds
    UInt8 NSAC;                   // The NSAC field in units of clock-cycle
    UInt8 MaxDataTransferRate;    // In units of kbit/s
    UInt16 CardCommandClasses;    // 12 bit command class support indicators
    UInt8 MaxReadBlockLength;     // In units of bytes
    UInt8 ReadBlockPartial;       // Partial Block Read allowed
    UInt8 WriteBlockMisalign;     // Misaligned Block Writes allowed
    UInt8 ReadBlockMisalign;      // Misaligned Block Reads allowed
    UInt8 DSRImplemented;         // DSR Register implemented
    UInt16 DeviceSize;            // Calculated device memory size in bytes
    UInt8 VDDReadCurrentMin;      // in units of milliamps
    UInt8 VDDReadCurrentMax;      // in units of milliamps
    UInt8 VDDWriteCurrentMin;     // in units of milliamps
    UInt8 VDDWriteCurrentMax;     // in units of milliamps
    UInt8 deviceSizeMultiplier;
    UInt8 EraseBlockEnable;       // Erase in multiples of MaxWriteBlockLength
    UInt8 EraseSectorSize;        // Size of an erase sector in blocks (SECTOR_SIZE+1)
    UInt8 WPGroupSize;            // Size of a WP group in erase sectors (WP_GRP_SIZE+1)
    UInt8 WPGroupEnable;          // Group Write Protection possible
    UInt8 WriteSpeedFactor;       // Block Write time as multiple of Read access time
    UInt8 MaxWriteBlockLength;    // In units of bytes
    UInt8 WriteBlockPartial;      // Partial block writes allowed
    UInt8 FileFormatGroup;        //File format group
    UInt8 CopyFlag;               // Copy control flag
    UInt8 PermanentWP;            // Permanent Write Protect flag
    UInt8 TemporaryWP;            // Temporary Write Protect flag
    UInt8 FileSystem;             // File System type
    UInt8 RE[2];                  //MAKE Align
} SD_PARSED_REGISTER_CSD, *PSD_PARSED_REGISTER_CSD;


typedef struct _SD_REGISTER_SCR {
    UInt8  SCRStructure;
    UInt8  SDSpec;
    UInt8  DataStatusAfterErases;
    UInt8  SDSecuritySupport;
    UInt8  DATBusWidthsSupported;
    UInt8  Reserved[6];
} SD_PARSED_REGISTER_SCR, *PSD_PARSED_REGISTER_SCR;
typedef UInt16 SD_CARD_RCA;         // card RCA
typedef UInt32  SD_CARD_STATUS;      // card status
#include "sderr.h"

#endif
// DO NOT REMOVE --- END EXTERNALLY DEVELOPED SOURCE CODE ID --- DO NOT REMOVE

